# 业务层实现登录、注册
import orm
import time


def show_data():
    path = r'book.json'  # 引用json文件
    book_list = orm.read_data(path)
    income1 = 0
    income2 = 0
    # for user in book_list:
    #     income=int(user['金额'])
    #     # print(income)
    #     # print(type(income))
    #     if income>=0:
    #         # print(income)
    #         income1+=income
    #     if income<0:
    #         # print(income)
    #         income2+=income
    for book in book_list:
        if book['分类'] == '收入':
            income1 += int(book['金额'])
        elif book['分类'] == '支出':
            income2 += int(book['金额'])
    #         break
    print(f'你的收入为：{income1}')
    print(f'你的支出为：{income2}')
    print(f'你的余额为：{income1-income2}')


def write_data():
    path = r'book.json'
    time1 = time.strftime('%Y/%m/%d %H:%M:%S')
    project = input('请输入你的收支的项目：')
    expend = input('请输入你的收支（收入或支出）：')
    money = float(input('请输入收支的金额:'))
    new_bill = {"时间": time1, "项目": project, "分类": expend, "金额": money}
    user_list = orm.read_data(path)
    user_list.append(new_bill)
    orm.write_data(user_list, path)
    print('添加成功！')


if __name__ == '__main__':
    write_data()
    # show_data()
